<template>
  <div class="case">
    <na-table
      :datas.sync="tableData"
      :searchObj.sync="search"
      @newAdd="addInventory"
      @updateDatas="getAllLists"
    >
    </na-table>
    <!--  盘点新建  -->
    <el-dialog
      :title="addTitle"
      :visible.sync="isAddInventory"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog78"
      width="85%"
      height="78%"
      @open="handleOpenDialog"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"
            ><span class="mr5">盘点单号：</span
            >{{ inventoryForm.takeStockBillNo }}</span
          >
          <span class="mr15"
            ><span class="mr5">创建日期：</span
            >{{
              inventoryForm.createTime || dayjs().format("YYYY-MM-DD")
            }}</span
          >
          <span
            ><span class="mr5">创建人员：</span
            >{{
              addTitle == "新建盘点" ? name : inventoryForm.createPersonName
            }}</span
          >
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="inventoryForm"
            :model="inventoryForm"
            label-width="80px"
            style="height: 70px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-form-item label="盘点备注" class="inlineText">
              <el-input
                v-model="inventoryForm.takeStockRemark"
                type="textarea"
                :rows="3"
                placeholder="盘点备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox flex flex_column flex1">
            <div class="tt flex flex_between">
              <div class="flex">
                <div style="width: 100px">
                  <span class="fw">盘点明细</span>
                </div>
                <el-input
                  placeholder="请输入内容"
                  v-model="searchDetail.likeWord"
                  size="mini"
                  class="input-with-select"
                >
                  <el-select
                    v-model="searchDetail.searchField"
                    slot="prepend"
                    filterable
                    style="width: 100px"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in searchData"
                      :key="index"
                      :label="item.title"
                      :value="item.field"
                    ></el-option>
                  </el-select>
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="handleSearchEvent"
                  ></el-button>
                </el-input>
              </div>
              <div class="composeRadio mr15 flex">
                <div class="">
                  <el-radio-group
                    v-model="searchInfo.takeStockState"
                    size="mini"
                    @change="stateQuery"
                  >
                    <el-radio-button label="0">待盘点</el-radio-button>
                    <el-radio-button label="1">已盘点</el-radio-button>
                  </el-radio-group>
                </div>
              </div>
            </div>
            <div class="GtableCon flex1" ref="GtableCon">
              <el-table
                v-loading="detailLoading"
                :data="inventoryDetailList"
                :row-class-name="tableRowClassName"
                :summary-method="getSummaries"
                id="myTableId"
                show-summary
                ref="detailTable1"
                border
                height="469"
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  label="规格编码"
                  prop="stockGoodsCode"
                  show-overflow-tooltip
                  min-width="110"
                >
                  <template scope="{row}">
                    {{ row.specificationCode }}
                  </template>
                </el-table-column>
                <el-table-column
                  label="物料名称"
                  prop="goodsName"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="规格型号"
                  prop="stockGoodsName"
                  min-width="190"
                />
                <el-table-column
                  label="物料分组"
                  prop="groupName"
                  min-width="90"
                />
                <el-table-column
                  label="物料类型"
                  prop="categoryName"
                  min-width="90"
                />
                <el-table-column
                  label="供应商"
                  prop="supplierName"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="生产批次号"
                  prop="productionBatchNumber"
                  show-overflow-tooltip
                  min-width="100"
                />
                <el-table-column
                  label="生产日期"
                  prop="productionDate"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="库区库位"
                  prop="warehouseArea"
                  show-overflow-tooltip
                  min-width="90"
                >
                  <template slot-scope="scope">
                    {{ scope.row.warehouseArea + scope.row.areaShelf }}
                  </template>
                </el-table-column>
                <el-table-column
                  label="物料单位"
                  prop="goodsUnitName"
                  min-width="80"
                />
                <el-table-column
                  label="每米/m²"
                  prop="specificationJsValue"
                  min-width="80"
                />
                <el-table-column
                  label="当前库存数量"
                  prop="originalStockQuantity"
                  min-width="110"
                />
                <el-table-column
                  label="盘点单价"
                  show-overflow-tooltip
                  prop="takeStockPrice"
                  min-width="90"
                />
                <el-table-column
                  label="库存总金额"
                  show-overflow-tooltip
                  prop="originalStockSumValue"
                  min-width="100"
                />
                <el-table-column label="实盘数量" min-width="110">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.takeStockQuantity"
                      @input="handleNumChange(scope.row, scope.$index)"
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏结果"
                  show-overflow-tooltip
                  prop="takeStockResult"
                  min-width="90"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1"> 持平 </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">盘赢</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">盘亏</div>
                    </template>
                    <template v-else>
                      <div>未盘</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏数量"
                  show-overflow-tooltip
                  prop="gainorLossQuantity"
                  min-width="90"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1"> 0 </template>
                    <template v-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossQuantity }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏总金额"
                  prop="gainorLossSumValue"
                  min-width="100"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1"> 0 </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">+ {{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossSumValue }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column label="备注" min-width="150">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.takeStockRemark"
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddInventory = false">取 消</el-button>
        <!-- 确认当前盘点     -->
        <el-button
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="warning"
          size="mini"
          @click="verifyTable(1)"
          >保存并继续
        </el-button>
        <!--  保存      -->
        <el-button
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="verifyTable(2)"
          >保存并关闭
        </el-button>
      </div>
    </el-dialog>
    <!--  查看盘点  -->
    <el-dialog
      title="查看盘点"
      :visible.sync="isSeeinventory"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"
            ><span class="mr5">盘点单号：</span
            >{{ inventoryForm.takeStockBillNo }}</span
          >
          <span class="mr15"
            ><span class="mr5">创建日期：</span
            >{{
              inventoryForm.createTime || dayjs().format("YYYY-MM-DD")
            }}</span
          >
          <span
            ><span class="mr5">创建人员：</span
            >{{ inventoryForm.createPersonName }}</span
          >
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="inventoryForm"
            :model="inventoryForm"
            disabled
            label-width="80px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-form-item label="盘点备注" class="inlineText">
              <el-input
                v-model="inventoryForm.takeStockRemark"
                type="textarea"
                :rows="1"
                placeholder="盘点备注"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox flex flex_column">
            <div class="tt flex flex_between">
              <div class="flex">
                <div style="width: 100px">
                  <span class="fw">盘点明细</span>
                </div>
                <el-input
                  placeholder="请输入内容"
                  v-model="searchDetail.likeWord"
                  size="mini"
                  class="input-with-select"
                >
                  <el-select
                    v-model="searchDetail.searchField"
                    slot="prepend"
                    filterable
                    style="width: 100px"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="(item, index) in searchData"
                      :key="index"
                      :label="item.title"
                      :value="item.field"
                    ></el-option>
                  </el-select>
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="handleSearchEvent"
                  ></el-button>
                </el-input>
              </div>
              <div class="composeRadio mr15 flex">
                <div class="combefore flex">
                  <el-radio-group
                    v-model="searchInfo.isTakeStock"
                    size="mini"
                    @change="stateQuerySee"
                  >
                    <el-radio-button label="0">
                      待盘点({{
                        inventoryForm.statData
                          ? inventoryForm.statData.waitTakeStockQuantity
                          : 0
                      }})</el-radio-button
                    >
                    <el-radio-button label="1">
                      已盘点({{
                        inventoryForm.statData
                          ? inventoryForm.statData.finishedTakeStockQuantity
                          : 0
                      }})
                    </el-radio-button>
                  </el-radio-group>
                </div>
                <div class="comafter flex">
                  <el-radio-group
                    v-model="searchInfo.takeStockResult"
                    size="mini"
                    @change="resultQuery"
                  >
                    <el-radio-button label="2"
                      >盘赢({{
                        inventoryForm.statData
                          ? inventoryForm.statData.panYingQuantity
                          : 0
                      }})
                    </el-radio-button>
                    <el-radio-button label="3"
                      >盘亏({{
                        inventoryForm.statData
                          ? inventoryForm.statData.panKuiQuantity
                          : 0
                      }})
                    </el-radio-button>
                    <el-radio-button label="1"
                      >持平({{
                        inventoryForm.statData
                          ? inventoryForm.statData.chiPingQuantity
                          : 0
                      }})
                    </el-radio-button>
                    <el-radio-button label="-1">
                      全部({{
                        inventoryForm.statData
                          ? inventoryForm.statData.allTakeStockQuantity
                          : 0
                      }})</el-radio-button
                    >
                  </el-radio-group>
                </div>
              </div>
            </div>
            <div class="GtableCon" ref="GtableConSee">
              <el-table
                v-loading="detailLoading"
                :data="inventoryDetailList"
                :row-class-name="tableRowClassName"
                :summary-method="getSummaries"
                ref="detailTable"
                show-summary
                border
                height="469"
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  label="规格编码"
                  prop="specificationCode"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="物料名称"
                  prop="goodsName"
                  show-overflow-tooltip
                  min-width="110"
                />
                <el-table-column
                  label="规格型号"
                  prop="stockGoodsName"
                  min-width="190"
                />
                <el-table-column
                  label="物料分组"
                  prop="groupName"
                  min-width="90"
                />
                <el-table-column
                  label="物料类型"
                  prop="categoryName"
                  min-width="90"
                />
                <el-table-column
                  label="供应商"
                  prop="supplierName"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="生产批次号"
                  prop="productionBatchNumber"
                  show-overflow-tooltip
                  min-width="100"
                />
                <el-table-column
                  label="生产日期"
                  prop="productionDate"
                  show-overflow-tooltip
                  min-width="90"
                />
                <el-table-column
                  label="库区库位"
                  prop="warehouseArea"
                  show-overflow-tooltip
                  min-width="90"
                >
                  <template slot-scope="scope">
                    {{ scope.row.warehouseArea + scope.row.areaShelf }}
                  </template>
                </el-table-column>
                <el-table-column
                  label="物料单位"
                  prop="goodsUnitName"
                  min-width="80"
                />
                <el-table-column
                  label="每米/m²"
                  prop="specificationJsValue"
                  min-width="80"
                />
                <el-table-column
                  label="当前库存数量"
                  prop="originalStockQuantity"
                  min-width="110"
                />
                <el-table-column
                  label="盘点单价"
                  show-overflow-tooltip
                  prop="takeStockPrice"
                  min-width="90"
                />
                <el-table-column
                  label="库存总金额"
                  show-overflow-tooltip
                  prop="originalStockSumValue"
                  min-width="100"
                />
                <el-table-column
                  label="实盘数量"
                  min-width="110"
                  prop="takeStockQuantity"
                />
                <el-table-column
                  label="盈亏结果"
                  show-overflow-tooltip
                  prop="takeStockResult"
                  min-width="90"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1">
                      {{ row.takeStockResultName }}
                    </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.takeStockResultName }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.takeStockResultName }}</div>
                    </template>
                    <template v-else>
                      <div>未盘</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏数量"
                  show-overflow-tooltip
                  prop="gainorLossQuantity"
                  min-width="90"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1"> 0 </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossQuantity }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossQuantity }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="盈亏总金额"
                  prop="gainorLossSumValue"
                  min-width="100"
                >
                  <template slot-scope="{ row }">
                    <template v-if="row.takeStockResult === 1"> 0 </template>
                    <template v-else-if="row.takeStockResult === 2">
                      <div class="c_54a55b">+ {{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else-if="row.takeStockResult === 3">
                      <div class="c_e15c45">{{ row.gainorLossSumValue }}</div>
                    </template>
                    <template v-else>
                      <div>{{ row.gainorLossSumValue }}</div>
                    </template>
                  </template>
                </el-table-column>
                <el-table-column
                  label="备注"
                  min-width="150"
                  prop="takeStockRemark"
                />
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSeeinventory = false">取 消</el-button>
        <!--  修改      -->
        <!-- <el-button
          v-if="ops==='see'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="updateClick"
        >修改
        </el-button> -->
        <!--  确认      -->
        <el-button
          v-if="ops === 'confirm'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="confirmClick"
          >确认</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arraySort } from "@/utils";
import naTable from "@/components/naTable";
import { mapGetters } from "vuex";
export default {
  name: "Inventory",
  components: {
    naTable,
  },
  computed: {
    ...mapGetters(["name"]),
  },
  data() {
    return {
      ops: "see", // 操作
      addTitle: "新建盘点",
      addLoading: false,
      detailLoading: false, // 盘点明细加载效果
      isAddInventory: false, // 新建盘点是否打开
      isSeeinventory: false, // 查看盘点弹框是否打开

      takeStockID: null, // 盘点主键ID
      searchDetail: {
        searchField: "goodsName",
        likeWord: "",
      },
      searchData: [
        { title: "规格编码", field: "specificationCode" },
        { title: "物料名称", field: "goodsName" },
        { title: "供应商", field: "supplierName" },
      ],
      inventoryForm: {
        warehouseCode: "",
        takeStockID: "",
        takeStockRemark: "",
        takeStockBillNo: "",
        detailList: [],
      }, // 新建盘点表单
      inventoryDetailList: [], // 盘点明细列表
      searchLoading: false, // 表格加载效果
      search: {
        pageIndex: 1,
        pageSize: 30,
        warehouseCode: "",
        takeStockState: "",
        takeStockResult: "",
      }, // 盘点分页列表搜索条件
      searchInfo: {
        takeStockBillNo: "", //盘点单号
        isTakeStock: "", //是否盘点：0待盘点；1已盘点，-1查询全部
        takeStockResult: "-1", //盘点结果：0未盘点；1盘平（无盈亏）；2盘盈；3盘亏，-1查询全部
        takeStockState: 0,
      }, // 盘点明细列表搜索条件
      detailList: [],
      arr1: [],
      arr2: [],
      total: 0,
      tableHead: [], // 表头
      tableHeight: null,
      jcINfoTableHeight: null,

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "takeStockID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        addPermission: "MenuCode5-8-1", //新建按钮权限
        voidWord: "", //需要查询作废的字段
        statusList: [], //状态筛选条件列表
        statusWord: "warehouseCode", //状态筛选的字段
        //表格按钮
        operationBtn: [
          {
            title: "查看", //按钮名称
            methodsName: "seeInventory", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "takeStockState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                1, 2,
              ],
            },
          },
          {
            title: "修改",
            methodsName: "modifyInventory",
            hideCondition: {
              //按钮显隐条件
              prop: "takeStockState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                1,
              ],
            },
          },
          {
            title: "完成盘点",
            methodsName: "confirmClick",
            hideCondition: {
              //按钮显隐条件
              prop: "takeStockState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                1,
              ],
            },
          },
          {
            title: "删除",
            methodsName: "deleteTakeStick",
            hideCondition: {
              //按钮显隐条件
              prop: "takeStockState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                1,
              ],
            },
          },
          {
            title: "打印盘点单",
            methodsName: "handleDownload",
            children:[{
              title: "全部",
              methodsName: "handleDownload",
            },{
              title: "盘盈",
              methodsName: "handleDownload",
            },{
              title: "盘亏",
              methodsName: "handleDownload",
            }]
          },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },
    };
  },
  created() {
    // 采购列表表格头部
    this.getTableHead();
    // 获取仓库下拉列表
    this.getWarehouseList();
  },
  mounted() {},
  methods: {

    //查看
    seeInventory(data) {
      this.ops = "see";
      this.isSeeinventory = true;
      this.inventoryForm.takeStockBillNo = data.takeStockBillNo;
      this.searchInfo.takeStockBillNo = data.takeStockBillNo;
      this.searchInfo.takeStockResult = "-1";
      this.getInventoryInfoList(data.takeStockBillNo);
      //this.getInventoryInfoList(data.takeStockBillNo)
      //   this.$api.warehouse.QueryTakeStockBillsMxList(this.searchInfo).then((res)=>{
      //   this.inventoryDetailList = res
      // })
      this.$nextTick(() => {
        this.jcINfoTableHeight = this.$refs.GtableConSee.offsetHeight;
      });
    },
    //修改
    modifyInventory(data) {
      // 修改
      this.isAddInventory = true;
      this.addTitle = "修改盘点";
      this.inventoryForm.takeStockBillNo = data.takeStockBillNo;
      this.getInventoryInfoList(data.takeStockBillNo);
      this.$nextTick(() => {
        this.jcINfoTableHeight = this.$refs.GtableCon.offsetHeight;
      });
    },

    //下载盘点单
    handleDownload(row,e) {
      let state = ''
      if(e.title == '全部'){
        state = -1
      }else if(e.title == '盘盈'){
        state = 2
      }else if(e.title == '盘亏'){
        state = 3
      }
      let parmas = {
        templateTypeCode: "MBLX0011", //	模板类型编号
        condition: {
          TakeStockBillNo: row.takeStockBillNo,
          TakeStockResult:state
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },
    //确认盘点
    confirmInventory(data) {
      // 确认盘点
      this.ops = "confirm";
      this.isSeeinventory = true;
      this.takeStockID = data.takeStockID;
      this.inventoryForm.takeStockBillNo = data.takeStockBillNo;
      this.getInventoryInfoList(data.takeStockBillNo);
      this.$nextTick(() => {
        this.jcINfoTableHeight = this.$refs.GtableConSee.offsetHeight;
      });
    },
    //实盘数量输入事件
    handleNumChange(row, index) {
      this.inventoryDetailList[index].gainorLossQuantity =
        row.takeStockQuantity - row.originalStockQuantity;

      this.inventoryDetailList[index].gainorLossSumValue = JSON.parse((
        row.gainorLossQuantity * row.takeStockPrice
      ).toFixed(4))

      if (this.inventoryDetailList[index].gainorLossQuantity < 0) {
        this.$set(this.inventoryDetailList[index],'takeStockResult',3)
        // this.inventoryDetailList[index].takeStockResult = 3;
      } else if (this.inventoryDetailList[index].gainorLossQuantity == 0) {
        // this.inventoryDetailList[index].takeStockResult = 1;
        this.$set(this.inventoryDetailList[index],'takeStockResult',1)
      } else {
        // this.inventoryDetailList[index].takeStockResult = 2;
        this.$set(this.inventoryDetailList[index],'takeStockResult',2)
      }
      this.$forceUpdate();
    },

    // 删除未确认的盘点单
    deleteTakeStick(data) {
      const parmas = {
        takeStockID: data.takeStockID,
      };
      this.$confirm("确认删除当前盘点, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.warehouse.DeleteTakeStock(parmas).then(() => {
          this.getAllLists();
          this.$message({
            message: "已删除",
            type: "success",
          });
        });
      });
    },
    // 确认盘点
    confirmClick(row) {
      const parmas = {
        takeStockID: row.takeStockID,
      };
      this.$confirm("确认当前盘点, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.warehouse.SureFinishTakeStock(parmas).then(() => {
          this.$message({
            message: "已确认",
            type: "success",
          });
          this.getAllLists();
          this.isSeeinventory = false;
        });
      });
    },
    updateClick() {
      this.isAddInventory = true;
      this.isSeeinventory = false;
      this.addTitle = "修改盘点";
    },

    verifyTable(type){
      this.inventoryForm.detailList = [...this.arr1, ...this.arr2];
      let refTab = this.$refs.detailTable1
      let refTabInsideData = refTab.data
      let state = true
      for(let i = 0; i < refTabInsideData.length; i++){
        let item = refTabInsideData[i]
        //获取到存在 "userNameId"这个属性的对象下标
        let tableIdLocation = i
        //获取到表格的节点,获取到表格的所有子节点
        let myTableId = document.getElementById("myTableId").childNodes

        //拿到第3个表格内容结构的所有子节点  class = "el-table__body-wrapper is-scrolling-none"
        let myTableIdChildNo3 =  myTableId[2].childNodes

        //在拿到所有子节点中的第一个
        let myTableIdChildNo3ChildNo1 = myTableIdChildNo3[0].childNodes

        //再拿到结构为<tboby></tboby>的节点
        let tbobyChildNo2 = myTableIdChildNo3ChildNo1[1]

        //获取到结构为<tboby></tboby>的子节点
        let kk = tbobyChildNo2.childNodes

        //把遍历处出来有 "userNameId:666" 这个字段的下标赋值给当前表格行
        let kkll = kk[tableIdLocation]

          if(item.takeStockResult === 3 && item.takeStockRemark === ''){
            //给这行表格动态的添加一个id,实现背景颜色的突出显示
            kkll.setAttribute('id','table_td_bg')
            console.log(i)
            //当点击"定位"按钮之后,实现屏幕滚动到这个id的位置,并且在屏幕的中间
            let element = document.getElementById("table_td_bg");
            element.scrollIntoView({block:"start"})
            state = false
            break
          }else{
            kkll.removeAttribute('id')
          }
      }
      if(!state){
        this.$message.warning('请填写备注！')
        return
      }
      if(type == 1){
        this.confirmAdd()
      }else if(type == 2){
        this.confirmAdd();
        this.isAddInventory = false;
      }
    },


    // 确认当前盘点
    confirmAdd() {
      if (this.addTitle == "新建盘点") {
        this.addLoading = true;
        const parmas = this.inventoryForm;
        this.$api.warehouse
          .AddTakeStock(parmas)
          .then((res) => {
            this.inventoryForm.takeStockBillNo = res;
            this.$message.success("创建成功");
            this.getInventoryInfoList(res);
            this.stateQuery()
            this.addTitle = "修改盘点";
            this.getAllLists();
            this.addLoading = false;
          })
          .catch(() => {
            this.addLoading = false;
          });
      } else if (this.addTitle == "修改盘点") {
        this.addLoading = true;
        const parmas = this.inventoryForm;
        this.$api.warehouse
          .UpdateTakeStockBill(parmas)
          .then((res) => {
            this.inventoryForm.takeStockBillNo = res;
            this.$message.success("修改成功");
            this.getInventoryInfoList(res);
            this.stateQuery()
            this.getAllLists();
            this.addLoading = false;
          })
          .catch(() => {
            this.addLoading = false;
          });
      }
    },
    // 合计方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          column.label !== "盈亏结果" &&
          column.label !== "规格型号" &&
          column.label != "每米/m²" &&
          column.label !== "物料名称" &&
          column.label !== "生产批次号" &&
          column.label !== "生产日期" &&
          column.label !== "库区库位"
        ) {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            if (sums[index]) {
              // if(String(sums[index]).indexOf('.') > -1){
              //   console.log(sums[index])
              //   sums[index] = String(sums[index]).split('.')[1]
              //   debugger
              //   if(sums[index].length>4){
              //     sums[index] = Number(sums[index]).toFixed(4)
              //   }
              // }else{
              //   sums[index] = sums[index]
              // }
              sums[index] = parseFloat(sums[index].toFixed(4));
            }
          } else {
            sums[index] = " ";
          }
        }
      });
      this.$nextTick(() => {
        this.$refs["detailTable1"].doLayout();
      });

      return sums;
    },
    // 给选中一行添加class
    tableRowClassName({ row }) {
      if (row.takeStockResult === 1) {
        return "";
      } else if (row.takeStockResult === 2) {
        return "green";
      } else if (row.takeStockResult === 3) {
        return "red";
      } else {
        return "blue";
      }
    },
    // 仓库改变事件
    inventoryChange() {
      this.getAllLists();
      this.inventoryForm.warehouseCode = this.search.warehouseCode;
      this.getInventoryList();
    },
    

    //明细筛选
    handleSearchEvent() {
      let arr = [];
      if (this.searchDetail.likeWord) {
        arr = this.detailList.filter((item) => {
          if (
            item[this.searchDetail.searchField].indexOf(
              this.searchDetail.likeWord
            ) > -1
          ) {
            return item;
          }
        });
      } else {
        arr = this.detailList;
      }
      this.inventoryDetailList = JSON.parse(JSON.stringify(arr));
    },

    stateQuerySee(val) {
      this.searchInfo.takeStockResult = "";
      this.searchInfo.isTakeStock = val;
      this.$api.warehouse
        .QueryTakeStockBillsMxList(this.searchInfo)
        .then((res) => {
          this.inventoryDetailList = res;
        });
    },

    resultQuery(val) {
      if (val == -1) {
        this.searchInfo.isTakeStock = -1;
      } else {
        this.searchInfo.isTakeStock = 1;
      }
      this.searchInfo.takeStockBillNo = this.inventoryForm.takeStockBillNo;
      this.searchInfo.takeStockResult = Number(this.searchInfo.takeStockResult);
      this.$api.warehouse
        .QueryTakeStockBillsMxList(this.searchInfo)
        .then((res) => {
          this.inventoryDetailList = res;
        });
      // if (this.inventoryForm.takeStockBillNo) {
      //   this.getInventoryInfoList(this.inventoryForm.takeStockBillNo)
      // } else {
      //   this.inventoryDetailList = []
      // }
    },
    // 新建盘点
    addInventory() {
      this.$api.warehouse
        .JuadgeIsHaveTakeStockBill({
          warehouseCode: this.search.warehouseCode,
        })
        .then((res) => {
          this.isAddInventory = true;
          this.addTitle = "新建盘点";
          this.inventoryForm.takeStockRemark = "";
          this.getInventoryList();
          this.$nextTick(() => {
            this.jcINfoTableHeight = this.$refs.GtableCon.offsetHeight;
          });
        });
    },
    // 没有盘点单号，获取盘点明细列表
    getInventoryList() {
      this.detailLoading = true;
      const parmas = {
        warehouseCode: this.inventoryForm.warehouseCode,
      };
      this.$api.warehouse
        .GetWarehousesBatchStockGoods(parmas)
        .then((res) => {
          this.inventoryDetailList = res;
          this.detailList = res;

          this.arr1 = this.detailList.filter((item) => {
            return item.isTakeStock == 0;
          });
          this.arr2 = this.detailList.filter((item) => {
            return item.isTakeStock == 1;
          });
          this.detailLoading = false;
        })
        .catch(() => {
          this.detailLoading = false;
        });
    },
    // 有盘点单号，获取盘点明细列表
    getInventoryInfoList(id) {
      this.detailLoading = true;
      const parmas = {
        takeStockID: 0,
        takeStockBillNo: id,
      };
      this.$api.warehouse
        .GetSingleTakeStock(parmas)
        .then((res) => {
          this.inventoryForm = res;
          this.inventoryDetailList = res.detailList;
          this.detailList = res.detailList;
          this.arr1 = this.detailList.filter((item) => {
            return item.isTakeStock == 0;
          });
          this.arr2 = this.detailList.filter((item) => {
            return item.isTakeStock == 1;
          });
          console.log(this.arr1)
          this.stateQuery(this.searchInfo.takeStockState)
          this.detailLoading = false;
        })
        .catch(() => {
          this.detailLoading = false;
        });
    },

    // 盘点明细 筛选
    stateQuery(val) {
      console.log(val)
      this.searchInfo.takeStockResult = "";
      this.inventoryDetailList = [];
      if (val == 0) {
        this.inventoryDetailList = JSON.parse(JSON.stringify(this.arr1));
      } else {
        this.inventoryDetailList = JSON.parse(JSON.stringify(this.arr2))
      }
      this.$forceUpdate()
    },

    // 获取仓库下拉列表
    getWarehouseList() {
      this.$api.warehouse.getWarehouseList().then((res) => {
        if (res.length > 0) {
          res.forEach((item) => {
            item.value = item.warehouseCode;
            item.label = item.warehouseName;
          });
        }
        this.tableData.statusList = res;
        this.inventoryForm.warehouseCode = res[0].warehouseCode;
        this.search.warehouseCode = res[0].warehouseCode;
        // 盘点分页列表
        this.getAllLists();
      });
    },
    // 盘点表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.takeStockList,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
      });
    },
    // 盘点分页列表
    getAllLists() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.warehouse
        .GetMatchPageTakeStocks(parmas)
        .then((res) => {
          this.tableData.tbody = res.list;
          this.search.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
    // 关闭弹框事件
    closeDialog() {
      this.searchDetail.likeWord = ''
    },
    handleOpenDialog() {
      this.searchInfo.takeStockState = 0;
    },
  },
};
</script>
<style lang="scss" scoped>
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    .GtableCon {
      width: 100%;
    }

    .tt {
      width: 100%;
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  height: 38px;
}
</style>
